/* QtLua -- Lua bindings for Qt
   Copyright (C) 2011, Jarek Pelczar

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA

*/

#ifndef QLUASCRIPTVALUE_P_H
#define QLUASCRIPTVALUE_P_H

#include "qluascriptvalue.h"
#include "qlualinkedlist_p.h"
#include "../etc/lua.hpp"
#include <QSharedData>

class QLuaScriptEnginePrivate;

class QLuaScriptValueData : public QSharedData
{
public:
    enum {
        L_TSTACKREF           = -10,
        L_TNONE               = LUA_TNONE,
        L_TNIL                = LUA_TNIL,
        L_TBOOLEAN	      = LUA_TBOOLEAN,
        L_TLIGHTUSERDATA      = LUA_TLIGHTUSERDATA,
        L_TNUMBER             = LUA_TNUMBER,
        L_TSTRING             = LUA_TSTRING,
        L_TTABLE              = LUA_TTABLE,
        L_TFUNCTION           = LUA_TFUNCTION,
        L_TUSERDATA           = LUA_TUSERDATA,
        L_TTHREAD             = LUA_TTHREAD,
        L_TINTERGER           = LUA_TNUMBER | 0x8000
    };

    QLuaInternal::QLinkedListElement<QLuaScriptValueData> m_listNode;
    QLuaScriptEnginePrivate * m_engine;
    int m_type;

    union SharedUnion {
        int m_handle;
        int m_int;
        qreal m_real;
        bool m_bool;
        void * m_ptr;
    };
    QByteArray m_str;
    SharedUnion u;

    QLuaScriptValueData();
    QLuaScriptValueData(const QLuaScriptValueData& other);
    ~QLuaScriptValueData();

    void clear();
    QVariant toVariant(const QLuaScriptValue * v_ptr) const;
    QVariant stackRefToVariant(const QLuaScriptValue * v_ptr) const;

    void push(lua_State * L);
    void fromStack(int index);
    void refStack(int index);

    QLuaScriptValue newEmpty();

    inline bool isRefObject() const {
        return m_type == L_TTABLE || m_type == L_TFUNCTION || m_type == L_TUSERDATA || m_type == L_TTHREAD;
    }
};

#endif // QLUASCRIPTVALUE_P_H
